package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MapperStatement;

import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    private Executor simpleExcutor = new SimpleExecutor();

    @Override
    public <E> List<E> selectAll(String statementId, Object... param) throws Exception {
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        List<E> query = simpleExcutor.query(configuration, mapperStatement, param);
        return query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectAll(statementId, params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("返回结果过多");
        }
    }
}
